package edu.must.uh.impl;

import java.util.List;
import java.util.Map;
import edu.must.uh.bean.DeployQueryResultBean;
import edu.must.uh.util.PageHelper;
import edu.must.uh.util.SqlUtils;
import edu.must.uh.util.Tools;
import edu.must.uh.util.ToolsOfString;

public class AttendancdCodeImpl extends BaseImpl {

    public AttendancdCodeImpl() {
         super.cashkey     = "attendance_code";
         super.cashListkey = "attendance_code";
         super.keyFields   = "ID";
         super.tableName   = "attendance_code"; 
    }
    
    public  DeployQueryResultBean getLimitlist(Map<String, Object> searchcondition,
            String sqlcondition) throws Exception {        
           
            String sqlString = "SELECT  ID,CODE,ISNIGHT,HOURS,TYPE,REMARK,PERIOD FROM "+ super.tableName +" Where DeleteMark= 0  ";
            sqlString = sqlString + Tools.getSQLCondition(searchcondition);
            sqlString = sqlString +ToolsOfString.NulltoEmpty(sqlcondition) ;     
            sqlString = sqlString + PageHelper.getLimit(_PageBean);
            DeployQueryResultBean resList = SqlUtils.getMySqlInstance().getLimitlist(sqlString);
            return resList;
    }
    
    
    
    public  Map<String,Object> getActuCode(Map<String, Object> searchcondition,
            String sqlcondition) throws Exception {        
        
           String ATTEND_DATE =ToolsOfString.NulltoEmpty(searchcondition.get("ATTEND_DATE"));
           String UserId =ToolsOfString.NulltoEmpty(searchcondition.get("UserId"));
           
            String sqlString = "SELECT " + 
                    "    a.ATTEND_DATE, " + 
                    "    IFNULL( c.CODE, a.`CODE` ) AS CODE, " + 
                    "    IFNULL( c.`PERIOD`, a.`PERIOD` ) AS PERIOD, " + 
                    "    IFNULL( c.TYPE, 1 ) AS TYPE, " + 
                    "    IFNULL( c.TIMETYPE, 1 ) AS TIMETYPE  " + 
                    " FROM " + 
                    "    attendance_days a " + 
                    "    LEFT JOIN ( " + 
                    "SELECT " + 
                    "    b.`UserId`, " + 
                    "    b.ATTEND_DATE, " + 
                    "    b.`CODE`, " + 
                    "    b.`PERIOD`, " + 
                    "    b.TYPE, " + 
                    "    b.TIMETYPE , " + 
                    "    b.ENABLEMARK  " + 
                    "FROM " + 
                    "    attendance_adjustment AS b " + 
                    "WHERE " + 
                    "    NOT EXISTS ( SELECT 1 FROM attendance_adjustment WHERE ATTEND_DATE = b.ATTEND_DATE AND UserId = b.UserId AND ENABLEMARK = 1 AND b.createTime < createTime )  " + 
                    "    ) c ON ( a.ATTEND_DATE = c.ATTEND_DATE AND a.UserId = c.UserId AND c.ENABLEMARK = 1  ) " + 
                    "WHERE " + 
                    "    a.ATTEND_DATE = '"+ATTEND_DATE+"'  " + 
                    "    AND a.UserId = '"+UserId+"' ";
           
            return SqlUtils.getMySqlInstance().getMap(sqlString);           
        }
    
    
    public  List<Map<String,Object>> getlistSelect(Map<String, Object> searchcondition,
            String sqlcondition) throws Exception {        
           
            String sqlString = "SELECT  ID,CODE,PERIOD,TYPE FROM "+ super.tableName +" Where DeleteMark= 0  ";
            sqlString = sqlString + Tools.getSQLCondition(searchcondition);
            sqlString = sqlString +ToolsOfString.NulltoEmpty(sqlcondition) ;  
            return SqlUtils.getMySqlInstance().getlist(sqlString);           
        }
    
}
